/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;
 
/*
 * Text example snippet: resize a text control (show about 10 characters)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

public class Snippet55 {
	
//	public static   Color selection =new Color(null,255,0,0);

public static void main (String [] args) {
	Display display = new Display ();
	final Shell shell = new Shell (display);
	shell.setLayoutData(new GridData(GridData.FILL_VERTICAL));
	shell.setLayout(new GridLayout());
	Composite composite = new Composite(shell,SWT.NULL| SWT.BORDER_SOLID );
	composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
	composite.setLayout(new GridLayout());
	
	Label test = new Label(composite,SWT.NULL);
	test.setText("Text");
	final Text text = new Text (composite, SWT.BORDER);
//	int columns = 10;
//	GC gc = new GC (text);
//	FontMetrics fm = gc.getFontMetrics ();
//	int width = columns * fm.getAverageCharWidth ();
//	int height = fm.getHeight ();
	composite.addPaintListener(new PaintListener() {
		    public void paintControl(PaintEvent e) {
		     GC gc=e.gc;
		     Color selection =new Color(null,255,0,0);
		      gc.setBackground(selection);
		     Rectangle rText=text.getBounds();
		      Rectangle rect1 = new Rectangle(rText.x-1, rText.y-1, 
		        rText.width+2, rText.height+2);
		
		      gc.fillRectangle(rect1);
		      shell.addControlListener(new ControlAdapter() {
		        public void controlResized(ControlEvent e) {
		          super.controlResized(e);               }
		      });
		    }
		  });
	text.addVerifyListener(new VerifyListener(){

		public void verifyText(VerifyEvent e) {
                 if(e.character==':')			
                	 e.doit = false;
                 System.out.println("typing "+e.character);
                 GC gc = new GC (shell);
    		     Color selection =new Color(null,0,0,255);
    		      gc.setBackground(selection);
    		     Rectangle rText=text.getBounds();
    		      Rectangle rect1 = new Rectangle(rText.x-1, rText.y-1, 
    		        rText.width+2, rText.height+2);
    		
    		      gc.fillRectangle(rect1);
		}
		
	});
//	gc.dispose ();
	text.setSize (100,12);
	//shell.pack ();
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
} 

